#include <iostream>
#include <arpa/inet.h>
#include <sys/timerfd.h>
#include "../tcp/TcpServer.h"
#include "../tcp/http/HttpServer.h"
#include "../tcp/Channel.h"
#include "../tcp/EventLoop.h"
#include "../base/thread/Thread.h"
#include "../base/thread/ThreadPool.h"
#include "../base/log/Logging.h"
#include "../base/log/LogStream.h"
#include "../base/log/LogFile.h"

void doClose(const TcpConnectionPtr &conn) {
    fprintf(stderr, "关闭客户端 %s 的连接\n", conn->peerAddress().toIp().c_str());
}

void onConnection(const TcpConnectionPtr &conn) {
    if (conn->connected()) {
        fprintf(stderr, "new connection %s from %s : %d\n", conn->getName().c_str(),
                conn->peerAddress().toIp().c_str(),
                conn->peerAddress().toPort());
    }
}

void onMessage(const TcpConnectionPtr &conn, Buffer *buffer, Timestamp time) {
    string data = buffer->retrieveAllAsString();
    fprintf(stderr, "客户端 : %s : %d 发送的消息为 : \n%s\n",
            conn->peerAddress().toIp().c_str(),
            conn->peerAddress().toPort(),
            data.c_str());
    conn->send(data);
}

void testServer() {
    InetAddress listenAddr(8200);
    EventLoop loop;
    TcpServer server(&loop, listenAddr, "MyServer");

    server.setConnectionCallback(onConnection);
    server.setMessageCallback(onMessage);
    server.setCloseCallback(doClose);

    server.setThreadNum(2);

    server.start();
    loop.loop();
}

void onHttpCallback() {

}

void testHttpServer() {
    InetAddress listenAddr(8200);
    EventLoop loop;
    HttpServer server(&loop, listenAddr, "myHttp");
    server.setThreadNum(2);
    server.start();
    loop.loop();
}

EventLoop *g_loop;

void timeout(Timestamp timestamp) {
    fprintf(stderr, "%s\n", timestamp.toFormattedString(true).c_str());
    fprintf(stderr, "Timeout!\n");
    g_loop->quit();
}

void testTimeTask() {
    EventLoop loop;
    g_loop = &loop;
    int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
    Channel channel(&loop, timerfd);
    channel.setReadCallback(timeout);
    channel.enableReading();

    struct itimerspec howlong;
    bzero(&howlong, sizeof howlong);
    howlong.it_value.tv_sec = 3;
    timerfd_settime(timerfd, 0, &howlong, NULL);
    loop.loop();
    close(timerfd);
}

void threadFunc() {
    fprintf(stderr, "测试线程函数启动~~~\n");

    testTimeTask();

    fprintf(stderr, "测试线程结束\n");
}

void testThread() {
    Thread thread(threadFunc, string("testThread"));
    fprintf(stderr, "开始执行主线程\n");
    thread.start();
    thread.join();
    fprintf(stderr, "开始执行主线程倒计时\n");
    testTimeTask();
    fprintf(stderr, "主线程倒计时结束\n");
}

void task1() {
    fprintf(stderr, "线程任务运行, 当前线程名称为%s\n", CurrentThread::name());
}

void task2() {
    fprintf(stderr, "线程任务运行2, 当前线程名称为%s\n", CurrentThread::name());
}

void testThreadPool() {
    ThreadPool pool("myThread");
    pool.start(1);
    pool.setMaxQueueSize(2);
    fprintf(stderr, "主线程创建, 线程名称为%s\n", CurrentThread::name());
    pool.run(task1);
    pool.run(task2);
    testTimeTask();
    pool.stop();
}

void testSimTcp() {
    InetAddress serverAddr(8200);
    Socket serverSocket(::socket(serverAddr.family(), SOCK_STREAM, IPPROTO_TCP));
    serverSocket.bindAddress(serverAddr);
    serverSocket.listen();
    InetAddress clientAddr;
    serverSocket.accept(&clientAddr);

    char cli_IP[BUFSIZ];
    fprintf(stderr, "连接到的客户端的 IP : %s, port 为 : %d\n", clientAddr.toIp().c_str(), clientAddr.toPort());
    Buffer buffer;
    int err = 0;
}


int main() {
    LOG_INFO << "程序启动ing";

    Logger::setLogLevel(Logger::INFO);

    testServer();
    //testHttpServer();

    LOG_INFO << "Hello World";

}
